package com.iot08.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.iot08.api.annotation.CheckAppSign;
import com.iot08.api.controller.base.BaseController;
import com.iot08.api.service.*;
import com.iot08.api.service.supplement.CommonService;
import com.iot08.api.vo.request.*;
import com.iot08.api.vo.request.base.BaseAccountReqVO;
import com.iot08.common.entity.app.Account;
import com.iot08.common.utils.StringUtil;
import com.iot08.common.utils.response.ResponseEnum;
import com.iot08.common.utils.response.ResponseMap;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

/**
 * @version 1.0
 * @Description: 账号相关接口
 * @time: 2020/11/3 16:33
 * @author: Rock
 */
@Slf4j
@RestController
@RequestMapping("getway/accounts")
public class AccountController extends BaseController {

    @Resource
    private AccountService accountService;
    @Resource
    private AdverstersService adverstersService;

    @Resource
    private DeviceMessageService deviceMessageService;
    @Resource
    private DeviceAttentionService deviceAttentionService;
    @Resource
    private DeviceNotificationService deviceNotificationService;
    @Resource
    private DeviceService deviceService;
    @Resource
    private AdverstersComInfoService adverstersComInfoService;
    @Resource
    private DeviceVideoService deviceVideoService;
    @Resource
    private CommonService commonService;
    @Resource
    private DeviceLastMissedVideoCallService deviceLastMissedVideoCallService;

    @Resource
    private SmsService smsService;

    @Resource
    private ScreensaverLibraryService screensaverLibraryService;

    @GetMapping("country")
    public ResponseMap getCountry() {

        return accountService.getCountry();
    }


    /**
     * 注册 (后续升级后将废弃)
     *
     * @param vo
     * @return
     */
    @PostMapping("register")
    @CheckAppSign
    public ResponseMap register(@Validated @RequestBody AccountsRegisVO vo) {
        return accountService.register(vo.getBodyForm(), false);
    }

    /**
     * 注册邮箱时 获取 注册码
     *
     * @param vo
     * @return
     */
    @GetMapping("registerLast")
    public ResponseMap getEmailCode(@Validated @RequestBody AccountsRegisVO vo) {
        return accountService.getEmailCode(vo.getBodyForm(), true);
    }

    /**
     * 注册
     *
     * @param vo
     * @time: 4/2/22 2:56 PM
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("registerLast")
    @CheckAppSign
    public ResponseMap registerLast(@Validated @RequestBody AccountsRegisVO vo) {
        return accountService.register(vo.getBodyForm(), true);
    }

    /**
     * 登录
     *
     * @param vo
     * @time: 2020/11/5 17:51
     * @author: Rock
     * @version 1.0
     */
    @PostMapping("login")
    @CheckAppSign
    public ResponseMap login(@Validated @RequestBody AccountsReqVO vo) {
        return accountService.login(vo.getBodyForm(), false);
    }

    /**
     * 登录(新)
     *
     * @param vo
     * @time: 4/2/22 4:50 PM
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("loginLast")
    @CheckAppSign
    public ResponseMap loginLast(@Validated @RequestBody AccountsReqVO vo) {
        return accountService.login(vo.getBodyForm(), true);
    }

    /**
     * 第三方登录  (后续升级后将废弃)
     *
     * @param vo
     * @time: 12/20/21 9:26 PM
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("")
    @CheckAppSign
    public ResponseMap s_login(@Validated @RequestBody AccountsReqVO vo) {
        return accountService.thirdlogin(vo.getBodyForm(), false);
    }

    /**
     * 第三方登录
     *
     * @param vo
     * @time: 12/20/21 9:26 PM
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("otherLogin")
    @CheckAppSign
    public ResponseMap otherLogin(@Validated @RequestBody AccountsReqVO vo) {
        return accountService.thirdlogin(vo.getBodyForm(), true);
    }

    /**
     * 第三方登录:判断手机号是否能绑定
     *
     * @param phone
     * @param bindCheckType
     * @time: 12/20/21 9:26 PM
     * @author: Weichao
     * @version 1.0
     */
    @GetMapping("bindPhone")
    public ResponseMap bindPhoneGet(@RequestParam(required = true) String phone,
                                    @RequestParam(required = true) Integer bindCheckType) {
        return accountService.bindPhoneGet(phone, bindCheckType);
    }

    /**
     * 第三方登录:绑定手机号
     *
     * @param vo
     * @param bindingResult
     * @time: 12/20/21 9:26 PM
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("bindPhone")
    public ResponseMap bindPhonePost(@Validated AccountsBindPhoneReqVO vo, BindingResult bindingResult) {
        return accountService.bindPhonePost(vo);
    }

    /**
     * 用户绑定第三方账号
     *
     * @param vo
     * @param bindingResult
     * @time: 12/20/21 9:26 PM
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("bindOtherAccount")
    public ResponseMap bindOtherAccount(@Validated AccountsBindOtherReqVO vo, BindingResult bindingResult) {

        return accountService.bindOtherAccount(vo);
    }

    /**
     * 用户解绑第三方账号
     *
     * @param vo
     * @param bindingResult
     * @time: 12/20/21 9:26 PM
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("unbindOtherAccount")
    public ResponseMap unbindOtherAccount(@Validated AccountsUnBindOtherReqVO vo, BindingResult bindingResult) {
        return accountService.unbindOtherAccount(vo);
    }


    /**
     * 重置账号密码 (后续升级后将废弃)
     *
     * @param vo
     * @time: 2020/11/5 17:55
     * @author: Rock
     * @version 1.0
     */
    @PostMapping("resetPassword")
    @CheckAppSign
    public ResponseMap resetPassword(@Validated @RequestBody ResetPasswordReqVO vo, BindingResult bindingResult) {
        return accountService.resetPassword(vo.getBodyForm(), false);
    }

    /**
     * 重置账号密码
     *
     * @param vo
     * @time: 2020/11/5 17:55
     * @author: Rock
     * @version 1.0
     */
    @PostMapping("resetPasswordLast")
    @CheckAppSign
    public ResponseMap resetPasswordLast(@Validated @RequestBody ResetPasswordReqVO vo, BindingResult bindingResult) {
        return accountService.resetPassword(vo.getBodyForm(), true);
    }


    /**
     * 修改手机号
     *
     * @param vo
     * @time: 2021-10-21 10:16
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("changeAccountLoginIdent")
    public ResponseMap changeAccountLoginIdent(@Validated AccountChangeLoginIdentReqVO vo, BindingResult bindingResult) {
        return accountService.changeAccountLoginIdent(vo);
    }


    /**
     * 密码校验
     *
     * @param paramsMap
     * @time: 2021-10-21 10:18
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("passwordAuth")
    public ResponseMap passwordAuth(AccountPasswordReqVO paramsMap) {

        return accountService.passwordAuth(paramsMap);
    }


    /**
     * 手机号校验
     *
     * @param paramsMap
     * @time: 2021-10-21 10:19
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("phoneOrEmailAuth")
    public ResponseMap phoneOrEmailAuth(AccountPhoneOrEmailReqVO paramsMap) {

        return accountService.phoneOrEmailAuth(paramsMap);
    }

    /**
     * 账号是否存在校验
     *
     * @param account 账号
     * @param type    账号类型 : 0:手机号 1:邮箱
     * @time: 2021-10-21 10:19
     * @author: Weichao
     * @version 1.0
     */
    @GetMapping("accountExists")
    public ResponseMap accountExists(@RequestParam(required = true) String account,
                                     @RequestParam(required = true) Integer type) {
        return accountService.accountExists(account, type);
    }

    /**
     * 获取账号信息 (后续升级后将废弃)
     *
     * @param openid
     * @time: 2020/11/6 17:25
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("{openid}")
    public ResponseMap getInfo(@PathVariable("openid") String openid) {
        return accountService.getInfo(openid);
    }

    /**
     * 更新账号信息 (后续升级后将废弃)
     *
     * @param
     * @param vo
     * @time: 2020/11/6 17:25
     * @author: Rock
     * @version 1.0
     */
    @PatchMapping(value = "/{openid}", consumes = "multipart/form-data")
    @CheckAppSign
    public ResponseMap update(@Validated @RequestPart("vo") String vo, MultipartFile image) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        vo = this.encodeVo(vo, objectMapper);
        AccountUnpdateReqVO arVo = objectMapper.readValue(vo, AccountUnpdateReqVO.class);
        return accountService.updateInfo(arVo.getBodyForm(), image, false);
    }

    /**
     * 微信绑定 (后续升级后将废弃)
     *
     * @param vo
     * @time: 2/26/22 5:04 PM
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("/{openid}")
    public ResponseMap wxBind(@Validated WxBindReqVO vo) throws IOException {
        return accountService.wxBind(vo);
    }

    /**
     * 微信解绑 (后续升级后将废弃)
     *
     * @param vo
     * @time: 2020/11/9 14:49
     * @author: Rock
     * @version 1.0
     */
    @DeleteMapping("/{openid}")
    public ResponseMap wxUnbind(@Validated BaseAccountReqVO vo) {
        return accountService.wxUnbind(vo);
    }


    /**
     * 获取账号信息
     *
     * @param openid
     * @time: 4/26/22 5:02 PM
     * @author: Weichao
     * @version 1.0
     */
    @GetMapping("/{openid}/updateInfo")
    public ResponseMap getInfo_new(@PathVariable("openid") String openid) {
        return accountService.getInfo(openid);
    }

    /**
     * 更新账号信息
     *
     * @param vo
     * @param image
     * @time: 4/26/22 4:51 PM
     * @author: Weichao
     * @version 1.0
     */
    @PatchMapping(value = "/{openid}/updateInfo", consumes = "multipart/form-data")
    @CheckAppSign
    public ResponseMap updateInfo(@Validated @RequestPart("vo") String vo, MultipartFile image) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        vo = this.encodeVo(vo, objectMapper);
        AccountUnpdateReqVO arVo = objectMapper.readValue(vo, AccountUnpdateReqVO.class);
        return accountService.updateInfo(arVo.getBodyForm(), image, true);
    }

    @PostMapping(value = "/{openid}/updateInfo/update", consumes = "multipart/form-data")
    @CheckAppSign
    public ResponseMap updateInfo_A(@Validated @RequestPart("vo") String vo, MultipartFile image) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        vo = this.encodeVo(vo, objectMapper);
        AccountUnpdateReqVO arVo = objectMapper.readValue(vo, AccountUnpdateReqVO.class);
        return accountService.updateInfo(arVo.getBodyForm(), image, true);
    }

    @PostMapping(value = "/{openid}/updateInfo/update_last", consumes = "multipart/form-data")
    @CheckAppSign
    public ResponseMap updateInfoLast(@Validated @RequestPart("vo") String vo, MultipartFile image) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        vo = this.encodeVo(vo, objectMapper);
        AccountUnpdateReqVO arVo = objectMapper.readValue(vo, AccountUnpdateReqVO.class);
        return accountService.updateInfo(arVo.getBodyForm(), image, false);
    }

    /**
     * 更新账号信息 (给不支持 PATCH的客户端使用)
     *
     * @param vo
     * @param
     * @param image
     * @time: 12/1/23 4:51 PM
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping(value = "/{openid}/updateInfo_other", consumes = "multipart/form-data")
    @CheckAppSign
    public ResponseMap updateInfoOther(@Validated @RequestPart("vo") String vo, MultipartFile image) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        vo = this.encodeVo(vo, objectMapper);
        AccountUnpdateReqVO arVo = objectMapper.readValue(vo, AccountUnpdateReqVO.class);
        return accountService.updateInfo(arVo.getBodyForm(), image, true);
    }

    @PostMapping(value = "/{openid}/updateInfo_other_2", consumes = "multipart/form-data")
    @CheckAppSign
    public ResponseMap updateInfoOther2(@Validated @RequestPart("vo") String vo, MultipartFile image) throws IOException {
        ObjectMapper objectMapper = new ObjectMapper();
        vo = this.encodeVo(vo, objectMapper);
        AccountUnpdateReqVO arVo = objectMapper.readValue(vo, AccountUnpdateReqVO.class);
        return accountService.updateInfo(arVo.getBodyForm(), image, false);
    }

    private String encodeVo(String vo, ObjectMapper objectMapper) throws JsonProcessingException, UnsupportedEncodingException {
        JSONObject jsonObject = objectMapper.readValue(vo, JSONObject.class);
        JSONObject bodyForm = jsonObject.getJSONObject("bodyForm");
        String isIos = bodyForm.getString("isIos");
        if (!StringUtils.isEmpty(isIos) && isIos.equals("1")) {
            vo = new String(vo.getBytes("ISO-8859-1"), "UTF-8");
        }
        return vo;
    }

    /**
     * 微信绑定
     *
     * @param vo
     * @time: 4/26/22 5:03 PM
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("/{openid}/updateInfo")
    public ResponseMap wxBind_new(@Validated WxBindReqVO vo) throws IOException {
        return accountService.wxBind(vo);
    }

    /**
     * 微信解绑
     *
     * @param vo
     * @time: 4/26/22 5:03 PM
     * @author: Weichao
     * @version 1.0
     */
    @DeleteMapping("/{openid}/updateInfo")
    public ResponseMap wxUnbind_new(@Validated BaseAccountReqVO vo) {
        return accountService.wxUnbind(vo);
    }

    /**
     * 注销账号
     *
     * @param vo
     * @time: 2/26/22 5:02 PM
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("/cancellation")
    @CheckAppSign
    public ResponseMap cancellation(@Validated @RequestBody CancelAccountReqVo vo) {
        return accountService.del(vo.getBodyForm().getOpenid());
    }


    /**
     * 退出登录
     *
     * @param openid
     * @time: 2021/1/7 15:17
     * @author: Rock
     * @version 1.0
     */
    @DeleteMapping("/{openid}/logout")
    public ResponseMap logout(@PathVariable("openid") String openid) {
        return accountService.logout(openid);
    }

    /**
     * 微信登录后申请绑定手机号码
     *
     * @param vo
     * @time: 2/26/22 4:44 PM
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("/{openid}/apply_bind_phone")
    public ResponseMap apply_bind_phone(@Validated PhoneParamReqVO vo) throws IOException {
        return accountService.apply_bind_phone(vo);
    }

    /**
     * 扫码之后调用的接口
     *
     * @param vo            请求参数
     * @param bindingResult
     * @time: 2/26/22 2:14 PM
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("/{openid}/after")
    public ResponseMap after(ImeiParamReqVO vo, BindingResult bindingResult) throws IOException {
        return accountService.after(vo);
    }

    /**
     * 扫码后提交申请绑定
     * 如果扫码设备后, 后台返回设备有管理员,那么得使用此接口进行的 <申请绑定>
     *
     * @param vo
     * @param bindingResult
     * @time: 2/26/22 3:31 PM
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("/{openid}/apply_bind")
    public ResponseMap apply_bind(@Validated ApplyBindDeviceReqVO vo, BindingResult bindingResult) {
        return accountService.apply_bind(vo);
    }


    /**
     * 查询用户关联设备
     *
     * @param vo
     * @time: 2020/11/9 16:39
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("/{openid}/related-device")
    public ResponseMap getRelatedDevice(@RequestHeader(value = "User-Agent", required = false) String userAgent,
                                        @RequestHeader(value = "Version", required = false) String version,
                                        @RequestHeader(value = "iosProduction", required = false) String iosProduction,
                                        @RequestHeader(value = "packageName", required = false) String packageName,
                                        @Validated BaseAccountReqVO vo) {
        return accountService.getRelatedDevice(vo, userAgent, version, iosProduction, packageName);
    }

    /**
     * @param
     * @return com.iot08.common.utils.response.ResponseMap
     * @description 查询设备指令信息
     * @author cwj
     * @date @time 2025/1/14 11:41
     **/
    @GetMapping("/{openid}/queryDeviceFunction")
    public ResponseMap queryDeviceFunction(@Validated DeviceFunctionReqVo vo) {
        return accountService.queryDeviceFunction(vo);
    }

    /**
     * 添加关联设备（绑定设备）
     *
     * @param vo
     * @time: 2020/11/9 16:39
     * @author: Rock
     * @version 1.0
     */
    @PostMapping(value = "/{openid}/related-device", consumes = "multipart/form-data")
    @CheckAppSign
    public ResponseMap addRelatedDevice(@Validated @RequestPart("vo") String vo, MultipartFile image) {
        try {
            log.info("绑定设备接口调用，请求参数为：{}", vo);
            ObjectMapper objectMapper = new ObjectMapper();
            vo = this.encodeVo(vo, objectMapper);
            AddRelatedDeviceReqVO adVo = objectMapper.readValue(vo, AddRelatedDeviceReqVO.class);
            return accountService.addRelatedDevice(adVo.getBodyForm(), image, true);
        } catch (IOException e) {
            log.error("绑定设备时发生异常，异常信息为：{}", e.getMessage());
            return ResponseMap.fail();
        }
    }

    /**
     * 解除设备关联
     *
     * @param vo
     * @time: 2020/11/11 11:46
     * @author: Rock
     * @version 1.0
     */
    @PostMapping("/{openid}/related-device/del")
    @CheckAppSign
    public ResponseMap delRelatedDevice(@Validated @RequestBody ImeiParamReqBdVO vo) {
        return accountService.delRelatedDevice(vo.getBodyForm());
    }

    /**
     * 查询用户聊天群组
     *
     * @param vo
     * @param bindingResult
     * @time: 2020/11/13 15:50
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("/{openid}/related-chat-group")
    public ResponseMap getRelatedChatGroup(@Validated UserChatGroupGetInfoReqVO vo, BindingResult bindingResult) {
        return accountService.getRelatedChatGroup(vo);
    }

    /**
     * 进入聊天群组
     *
     * @param vo
     * @time: 2020/11/9 16:39
     * @author: Rock
     * @version 1.0
     */
    @PostMapping("/{openid}/related-chat-group")
    public ResponseMap addRelatedChatGroup(@Validated AddRelatedChatGroupReqVO vo, BindingResult bindingResult) {
        return accountService.addRelatedChatGroup(vo);
    }

    /**
     * 退出聊天群组
     *
     * @param vo
     * @param bindingResult
     * @time: 2020/11/13 15:56
     * @author: Rock
     * @version 1.0
     */
    @DeleteMapping("/{openid}/related-chat-group")
    public ResponseMap delRelatedChatGroup(@Validated GroupidParamReqVO vo, BindingResult bindingResult) {
        return accountService.delRelatedChatGroup(vo);
    }

    /**
     * 获取消息中心数据 (告警)
     *
     * @param vo
     * @param bindingResult
     * @time: 2020/11/13 15:50
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("/{openid}/message")
    public ResponseMap getMessage(@Validated GetMessageReqVO vo, BindingResult bindingResult) {
        return deviceMessageService.list(vo);
    }

    /**
     * 删除消息中心数据 (告警)
     *
     * @param vo
     * @param bindingResult
     * @time: 2020/11/20 14:40
     * @author: Rock
     * @version 1.0
     */
    @DeleteMapping("/{openid}/message")
    public ResponseMap delMessage(@Validated MessageIdsParamReqVO vo, BindingResult bindingResult) {
        return deviceMessageService.dels(vo);
    }


    /**
     * 获取关注记录数据
     *
     * @param vo
     * @param bindingResult
     * @time: 2022/02/28 15:50
     * @author: Weichao
     * @version 1.0
     */
    @GetMapping("/{openid}/attention")
    public ResponseMap getAttention(@Validated GetAttentionReqVO vo, BindingResult bindingResult) {
        return deviceAttentionService.list(vo);
    }

    /**
     * 处理关注记录数据 (管理员)
     *
     * @param vo
     * @param bindingResult
     * @time: 2022/02/28 15:50
     * @author: Weichao
     * @version 1.0
     */
    @PostMapping("/{openid}/attention")
    public ResponseMap handleAttention(@Validated GetAttentionReqVO vo, BindingResult bindingResult) throws IOException {
        deviceAttentionService.handle(vo);
        return ResponseMap.success();
    }


    /**
     * 删除关注记录数据
     *
     * @param vo
     * @param bindingResult
     * @time: 2022/02/28 15:50
     * @author: Weichao
     * @version 1.0
     */
    @DeleteMapping("/{openid}/attention")
    public ResponseMap delAttention(@Validated GetAttentionReqVO vo, BindingResult bindingResult) {
        deviceAttentionService.dels(vo);
        return ResponseMap.success();
    }


    /**
     * 获取通知数据 (系统通知)
     *
     * @param vo
     * @param bindingResult
     * @time: 2020/11/20 15:40
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("/{openid}/notification")
    public ResponseMap getNotification(@Validated GetNotificationReqVO vo, BindingResult bindingResult) {
        return deviceNotificationService.list(vo);
    }

    /**
     * 删除通知数据 (系统通知)
     *
     * @param vo
     * @param bindingResult
     * @time: 2020/11/20 14:40
     * @author: Rock
     * @version 1.0
     */
    @DeleteMapping("/{openid}/notification")
    public ResponseMap delNotification(@Validated BaseAccountReqVO vo, BindingResult bindingResult) {
        return deviceNotificationService.delByOpenid(vo);
    }

    /**
     * 根据Openid查询当前是否正在视频
     *
     * @param vo
     * @param bindingResult
     * @time: 2020/12/25 11:45
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("/get_video_with_openid")
    public ResponseMap getVideoWithOpenid(@Validated BaseAccountReqVO vo, BindingResult bindingResult) {
        return accountService.getVideoWithOpenid(vo);
    }

    /**
     * 发起视频通话请求
     *
     * @param imei
     * @param vo
     * @param bindingResult
     * @time: 2021/1/6 14:03
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("/{imei}/video")
    public ResponseMap sendVideo(@PathVariable("imei") String imei,
                                 @Validated SendVideoReqVO vo, BindingResult bindingResult) {
        //log.info("sendVideo imei={},  vo = {}", imei, vo);
        return deviceVideoService.sendVideo(imei, vo, 0);
    }

    /**
     * 上传通话时长
     *
     * @param imei
     * @param vo
     * @param bindingResult
     * @time: 2021/1/14 17:38
     * @author: Rock
     * @version 1.0
     */
    @PostMapping("/{imei}/video")
    public ResponseMap updateDeviceVideo(@PathVariable("imei") String imei,
                                         @Validated UpdateDeviceVideoReqVO vo, BindingResult bindingResult) {

        if (StringUtil.isBlank(vo.getVideo_id())) {
            return ResponseMap.success();
        }
        return deviceVideoService.updateDeviceVideo(imei, vo.getVideo_id(), vo.getDuration(), vo.getWait_duration(), "APP",vo.getOpenid());
    }

    /**
     * 拒绝通话邀请
     *
     * @param imei
     * @param vo
     * @param bindingResult
     * @time: 2021/1/14 18:54
     * @author: Rock
     * @version 1.0
     */
    @DeleteMapping("/{imei}/video")
    public ResponseMap rejectDeviceVideo(@PathVariable("imei") String imei,
                                         @Validated RejectDeviceVideoReqVO vo, BindingResult bindingResult) {
        return deviceVideoService.rejectDeviceVideo(imei, vo,null);
    }

    /**
     * 加入群组通话状态
     */
    @PostMapping("/videoGroup/join")
    public void joinVideoRoom(@RequestParam String openid, @RequestParam String imei) {
        deviceVideoService.joinVideoRoom(openid, imei);
    }



    /**
     * 取消视频邀请
     *
     * @param imei
     * @param vo
     * @param bindingResult
     * @time: 2025/1/15 10:00
     * @author:
     * @version 1.0
     */
    @DeleteMapping("/{imei}/video/cancel")
    public ResponseMap cancelDeviceVideo(@PathVariable("imei") String imei,
                                         @Validated RejectDeviceVideoReqVO vo, BindingResult bindingResult) {
        Integer count = 0;
        return deviceVideoService.rejectDeviceVideo(imei, vo, count);
    }

    /**
     * 广告配置获取
     *
     * @param openid
     * @time: 2021/1/5 17:20
     * @author: Rock
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("/ads/{openid}")
    public ResponseMap getAdversters(@PathVariable("openid") String openid,
                                     @RequestHeader(value = "User-Agent", required = false) String userAgent,
                                     @RequestHeader(value = "Version", required = false) String version) {
        return adverstersService.getInfo(openid, userAgent, version);
    }

    /**
     * 获取通话续费套餐
     *
     * @param imei
     * @param vo
     * @param bindingResult
     * @time: 2021/1/6 10:54
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("/{imei}/video_renew")
    public ResponseMap getVideoRenew(@PathVariable("imei") String imei,
                                     @Validated BaseAccountReqVO vo, BindingResult bindingResult) {
        return deviceService.getVideoRenew(imei, vo);
    }

    /**
     * 获取当前用户是否有未读消息
     *
     * @param vo
     * @param bindingResult
     * @time: 2021/1/28 11:14
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("/unread_messages")
    public ResponseMap getUnReadMessages(@Validated UnReadMessagesReqVO vo, BindingResult bindingResult) {
        return deviceMessageService.getUnReadMessages(vo);
    }

    /**
     * POST、GET都兼容，客户端旧版本请求方式写错
     *
     * @param imei
     * @param vo
     * @param bindingResult
     * @time: 2021/3/25 15:27
     * @author: Rock
     * @version 1.0
     */
    @RequestMapping(value = "/{imei}/video_appnet_status", method = {RequestMethod.POST, RequestMethod.GET})
    public ResponseMap sendVideoAppnetStatus(@PathVariable("imei") String imei,
                                             @Validated SendVideoAppnetStatusReqVO vo, BindingResult bindingResult) {
        return commonService.sendVideoAppnetStatus(imei, vo);
    }

    /**
     * 查询未接的通话记录
     *
     * @param vo
     * @param bindingResult
     * @time: 2021/3/25 15:55
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("/get_last_missed_video_call")
    public ResponseMap getLastMissedVideoCall(@Validated BaseAccountReqVO vo, BindingResult bindingResult) {
        return deviceLastMissedVideoCallService.getLastMissedVideoCall(vo);
    }

    /**
     * 发送视频监控
     *
     * @param imei
     * @param vo
     * @param bindingResult
     * @time: 2021/6/1 11:41
     * @author: Rock
     * @version 1.0
     */
    @GetMapping("/{imei}/video_monito")
    public ResponseMap sendVideoMonito(@PathVariable("imei") String imei,
                                       @Validated SendVideoReqVO vo, BindingResult bindingResult) {
        return deviceVideoService.sendVideo(imei, vo, 1);
    }

    /**
     * 注销账号
     *
     * @param vo
     * @param bindingResult
     * @time: 2021/6/7 14:27
     * @author: Rock
     * @version 1.0
     */
    @DeleteMapping("/cancel")
    public ResponseMap cancel(@Validated AccountsCancelVO vo, BindingResult bindingResult) {
        return accountService.cancel(vo);
    }

    /**
     * 获取公众号模板消息开关状态
     *
     * @param openid
     * @param vo
     * @param bindingResult
     * @return
     */
    @GetMapping("/{openid}/vipcn")
    public ResponseMap vipcn(@PathVariable("openid") String openid,
                             @Validated BaseAccountReqVO vo, BindingResult bindingResult) {
        Account account = accountService.findById(openid);
        int status = 2;
        if (account.getSubscribe() == 1) {
            status = account.getWechat_send_status();
        }
        JSONObject res = new JSONObject();
        res.put("status", status);
        return ResponseMap.success(res);
    }

    /**
     * 设置公众号模板消息开关状态
     *
     * @param openid
     * @param vo
     * @param bindingResult
     * @return
     */
    @PostMapping("/{openid}/vipcn")
    public ResponseMap setVipcn(@PathVariable("openid") String openid, @Validated VipcnReqVO vo, BindingResult bindingResult) {
        Account account = accountService.findById(openid);
        if (account.getVipcn_openid() == null) {
            return ResponseMap.fail(ResponseEnum.STATUS403.getCode(), "请先关注公众号《赛诺汇》");
        }
        Update update = new Update();
        update.set("wechat_send_status", vo.getStatus());
        accountService.updateById(openid, update);
        log.info("openid：{} 设置公众号模板消息开关状态 成功", openid);
        return ResponseMap.success();

    }

    /**
     * @param
     * @return com.iot08.common.utils.response.ResponseMap
     * @description:发送短信
     * @author cwj
     * @date @time 2024/11/18 8:56
     **/
    @PostMapping(value = "/sendSms")
    @CheckAppSign
    public ResponseMap sendSmsCode(@Validated @RequestBody SmsReqVo vo) {
        return smsService.sendCode(vo.getBodyForm());
    }

    /**
     * @param
     * @return com.iot08.common.utils.response.ResponseMap
     * @description 检查用户是否存在
     * @author cwj
     * @date @time 2024/12/2 13:52
     **/
    @PostMapping({"/checkUserExists"})
    @CheckAppSign
    public ResponseMap chcekAccountExists(@Validated @RequestBody UserExitsReqVo vo) {
        return this.accountService.chcekAccountExists(vo.getBodyForm());
    }

    /**
     * @param
     * @return com.iot08.common.utils.response.ResponseMap
     * @description 保存屏保图库
     * @author cwj
     * @date @time 2024/12/21 11:52
     **/
    @PostMapping("/addScreensaverLibrary")
    public ResponseMap addScreensaverLibrary(@Validated ScreensaverLibraryReqVo vo) {
        return screensaverLibraryService.saveScreensaverLibrary(vo);
    }
}
